/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author risto
 */
public class CSVReaderImpl implements CSVReader
{
    BufferedReader br;

    public CSVReaderImpl(File f) throws FileNotFoundException
    {
        br = new BufferedReader(new FileReader(f));
    }

    @Override
    public List<String> readLine() throws IOException
    {
        ArrayList<String> al = new ArrayList<String>();
        while(true) {
            String line = br.readLine();
            if (line == null) return null;
            line = line.trim();
            if (line == null || line.length() == 0) continue; // empty line
            if (line.startsWith("#")) continue; // COMMENT

            String[] res = line.split(","); // !! not 100% correct, but will do (doesn't handle escaped commas)
            for (String s : res) al.add(s);
            return al;
        }
    }

    public void close() throws IOException
    {
        br.close();
        br = null;
    }
}
